package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import worldModel.TradeModel;

public class TradeView extends MasterViewPanel {

    private static final long serialVersionUID = -7240826712486524454L;
    private JButton quit;
    private JButton trade;
    private JButton host;
    private JButton participate;

    private JList inv;
    private JList giving;
    private JList receiving;

    private TradeModel model;

    MasterView master;

    public TradeView(MasterView m) {
	super(m);
	master = m;
	try {
	    model = new TradeModel(this, m);
	} catch (Exception e) {
	    System.out
		    .println("Something went wrong inside the new TradeModel.");
	}
	setUpLayout();
	setUpLabels();
	setUpLists();
	setUpButtons();
	setUpStatusLabel();
    }

    private void setUpLayout() {
	this.setLayout(new FlowLayout());
    }

    private void setUpStatusLabel() {
	JLabel status = new JLabel(
		"          The other user is not ready to trade...");
	this.add(status);
    }

    private void setUpButtons() {
	quit = new JButton("Quit");
	quit.addActionListener(new QuitListener());
	this.add(quit);
	trade = new JButton("Trade");
	trade.setBackground(new Color(255, 100, 100));
	trade.addActionListener(new TradeListener());
	this.add(trade);
	host = new JButton("Host Trade");
	host.addActionListener(new HostListener());
	this.add(host);
	participate = new JButton("Participate In Trade");
	participate.addActionListener(new ParticipateListener());
	this.add(participate);
    }

    private class QuitListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    model.Quit();
	    model.closeConnection();
	    master.changeView(Views.TITLE, null);
	}

    }

    private class TradeListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    model.ToggleTrade();
	}
    }

    private class ParticipateListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    try {
		model.connectToServer();
	    } catch (IOException e1) {
		e1.printStackTrace();
	    }
	}
    }

    private class HostListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    try {
		model.hostServer();
	    } catch (IOException e1) {
		e1.printStackTrace();
	    }
	}
    }

    private void setUpLabels() {
	JPanel invPanel = new JPanel();
	invPanel.setPreferredSize(new Dimension(200, 20));
	invPanel.add(new JLabel("Inventory"));
	this.add(invPanel);

	JPanel givingPanel = new JPanel();
	givingPanel.setPreferredSize(new Dimension(200, 20));
	givingPanel.add(new JLabel("Items You'll Give"));
	this.add(givingPanel);

	JPanel receivingPanel = new JPanel();
	receivingPanel.setPreferredSize(new Dimension(200, 20));
	receivingPanel.add(new JLabel("Items You'll Receive"));
	this.add(receivingPanel);
    }

    public void enableInvList() {
	inv.setEnabled(true);
    }

    public void disableInvList() {
	inv.setEnabled(false);
    }

    /*
     * The inv list will start disabled so the user doesn't try trading
     * immediately.
     */
    private void setUpLists() {
	inv = new JList(model.getNavigatorList().toArray());
	inv.addMouseListener(new InvClickListener());
	inv.setFixedCellWidth(180);
	JScrollPane invScroll = new JScrollPane(inv);
	invScroll.setPreferredSize(new Dimension(200, 300));
	disableInvList();
	this.add(invScroll);

	giving = new JList(model.getGivingList().toArray());
	giving.addMouseListener(new GivingClickListener());
	giving.setFixedCellWidth(180);
	JScrollPane givingScroll = new JScrollPane(giving);
	givingScroll.setPreferredSize(new Dimension(200, 300));
	this.add(givingScroll);

	receiving = new JList(model.getReceivingList().toArray());
	receiving.setFixedCellWidth(180);
	JScrollPane receivingScroll = new JScrollPane(receiving);
	receivingScroll.setPreferredSize(new Dimension(200, 300));
	this.add(receivingScroll);
    }

    public void repaintLists() {
	inv.setListData(model.getNavigatorList().toArray());
	giving.setListData(model.getGivingList().toArray());
	receiving.setListData(model.getReceivingList().toArray());
	inv.repaint();
	giving.repaint();
	receiving.repaint();
    }

    private class InvClickListener implements MouseListener {

	@Override
	public void mouseClicked(MouseEvent e) {
	    int index = inv.getSelectedIndex();
	    if (index < model.getNavigatorList().size() && index > -1) {
		model.getGivingList().add(
			model.getNavigatorList().remove(index));
		model.updateGivingData();
		repaintLists();
	    }
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
    }

    private class GivingClickListener implements MouseListener {

	@Override
	public void mouseClicked(MouseEvent e) {
	    int index = giving.getSelectedIndex();
	    if (index < model.getGivingList().size() && index > -1) {
		model.getNavigatorList().add(
			model.getGivingList().remove(index));
		model.updateGivingData();
		repaintLists();
	    }
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
    }
}